package andriod.action.doc;

import java.io.PrintWriter;
import java.sql.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import com.jcrj.action.BaseAction;
import com.jcrj.entity.OUser;
import com.jcrj.entity.OWorkflow;
import com.jcrj.service.FielTransactService;
import com.jcrj.service.TaskService;
import com.jcrj.service.WorkflowManagerService;
import com.jcrj.util.DateProcessor;
import com.jcrj.util.FielAll;

public class FielTransactAction extends BaseAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = 5138138830955493735L;
	private FielTransactService fielTransactService;
	private WorkflowManagerService workflowManagerService;
	private TaskService taskService;
	private List<FielAll> fielAll;
	private long totalCount;
	private long pageCount;
	private String name;
	private List<OWorkflow> flowList;

	public String guardFiel() {
		try {
			OUser user = (OUser) super.getSession().get("userInfo");
			String process = this.workflowManagerService.getSqlhis(user);
			this.fielAll = this.fielTransactService.getAllfiel(process,
					super.getCurrentPage(), this.name, null,
					super.getStartDate(), super.getEndDate());
			int i = 0;
			for (int sel = this.fielAll.size(); i < sel; i++) {
				if (isNumeric(((FielAll) this.fielAll.get(i)).getSender())) {
					String count = ((FielAll) this.fielAll.get(i)).getSender();
					((FielAll) this.fielAll.get(i)).setSender(this.taskService
							.getUserNameNew(
									Integer.parseInt(((FielAll) this.fielAll
											.get(i)).getSender())).getName());
					((FielAll) this.fielAll.get(i))
							.setUnit(this.fielTransactService
									.getUserAllOrgName(this.taskService
											.getUserNameNew(
													Integer.parseInt(count))
											.getOrgId().intValue()));
				}
				if (isNumeric(((FielAll) this.fielAll.get(i)).getFlow())) {
					OWorkflow flow = this.fielTransactService
							.getWorkFlow(Integer
									.parseInt(((FielAll) this.fielAll.get(i))
											.getFlow()));
					if ((((FielAll) this.fielAll.get(i)).getTitle() == null)
							|| ("".equals(((FielAll) this.fielAll.get(i))
									.getTitle()))) {
						((FielAll) this.fielAll.get(i))
								.setTitle(flow.getName());
					}
					((FielAll) this.fielAll.get(i)).setFlow(flow.getTypeName());
				}
			}
			this.totalCount = this.fielTransactService.countAllFiel(process,
					this.name, null, super.getStartDate(), super.getEndDate())
					.longValue();
			this.pageCount = ((this.totalCount - 1L) / 10L + 1L);
			
			
			JsonConfig config = new JsonConfig();
			config.registerJsonValueProcessor(Date.class, new DateProcessor());
			config.registerJsonValueProcessor(java.util.Date.class,
					new DateProcessor());
			JSONArray array = JSONArray.fromObject(fielAll,config);
			JSONObject obj = new JSONObject();
			obj.put("totalCount", totalCount);
			obj.put("pageCount", this.pageCount);
			obj.put("currentPage", super.getCurrentPage());
			obj.put("list", array);
			HttpServletResponse response = super.getResponse();
			response.setContentType("text/json;charset=utf-8");
			PrintWriter out = super.getResponse().getWriter();
			out.print(obj.toString());
			out.flush();
			
			
			
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public String waitFiel() {
		try {
			OUser user = (OUser) getSession().get("userInfo");
			List agent = this.taskService.getAgentUser(user);
			String process = this.workflowManagerService
					.getSqlProcessinstanceList(user.getId().intValue(), agent);
			this.fielAll = this.fielTransactService.getAllfiel(process,
					super.getCurrentPage(), this.name, null,
					super.getStartDate(), super.getEndDate());
			int i = 0;
			for (int sel = this.fielAll.size(); i < sel; i++) {
				if (isNumeric(((FielAll) this.fielAll.get(i)).getSender())) {
					String count = ((FielAll) this.fielAll.get(i)).getSender();
					((FielAll) this.fielAll.get(i)).setSender(this.taskService
							.getUserNameNew(
									Integer.parseInt(((FielAll) this.fielAll
											.get(i)).getSender())).getName());
					((FielAll) this.fielAll.get(i))
							.setUnit(this.fielTransactService
									.getUserAllOrgName(this.taskService
											.getUserNameNew(
													Integer.parseInt(count))
											.getOrgId().intValue()));
				}
				if (isNumeric(((FielAll) this.fielAll.get(i)).getFlow())) {
					OWorkflow flow = this.fielTransactService
							.getWorkFlow(Integer
									.parseInt(((FielAll) this.fielAll.get(i))
											.getFlow()));
					if ((((FielAll) this.fielAll.get(i)).getTitle() == null)
							|| ("".equals(((FielAll) this.fielAll.get(i))
									.getTitle()))) {
						((FielAll) this.fielAll.get(i))
								.setTitle(flow.getName());
					}
					((FielAll) this.fielAll.get(i)).setFlow(flow.getTypeName());
				}
			}
			this.totalCount = this.fielTransactService.countAllFiel(process,
					this.name, null, super.getStartDate(), super.getEndDate())
					.longValue();
			this.pageCount = ((this.totalCount - 1L) / 10L + 1L);
			JsonConfig config = new JsonConfig();
			config.registerJsonValueProcessor(Date.class, new DateProcessor());
			config.registerJsonValueProcessor(java.util.Date.class,
					new DateProcessor());
			JSONArray array = JSONArray.fromObject(fielAll,config);
			JSONObject obj = new JSONObject();
			obj.put("totalCount", totalCount);
			obj.put("pageCount", this.pageCount);
			obj.put("currentPage", super.getCurrentPage());
			obj.put("list", array);
			HttpServletResponse response = super.getResponse();
			response.setContentType("text/json;charset=utf-8");
			PrintWriter out = super.getResponse().getWriter();
			out.print(obj.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public String waitWorkFlowTaskFile() {
		try {
			this.flowList = this.workflowManagerService.workFlowTypeList();
			OUser user = (OUser) getSession().get("userInfo");
			List agent = this.taskService.getAgentUser(user);
			String process = this.workflowManagerService
					.getSqlProcessinstanceList(user.getId().intValue(), agent);
			this.fielAll = this.fielTransactService.getWorkFlowTaskfile(
					process, super.getCurrentPage(), this.name, null,
					super.getStartDate(), super.getEndDate());
			int i = 0;
			for (int sel = this.fielAll.size(); i < sel; i++) {
				if (isNumeric(((FielAll) this.fielAll.get(i)).getSender())) {
					String count = ((FielAll) this.fielAll.get(i)).getSender();
					((FielAll) this.fielAll.get(i)).setSender(this.taskService
							.getUserNameNew(
									Integer.parseInt(((FielAll) this.fielAll
											.get(i)).getSender())).getName());
					((FielAll) this.fielAll.get(i))
							.setUnit(this.fielTransactService
									.getUserAllOrgName(this.taskService
											.getUserNameNew(
													Integer.parseInt(count))
											.getOrgId().intValue()));
				}
				if (isNumeric(((FielAll) this.fielAll.get(i)).getFlow())) {
					OWorkflow flow = this.fielTransactService
							.getWorkFlow(Integer
									.parseInt(((FielAll) this.fielAll.get(i))
											.getFlow()));
					((FielAll) this.fielAll.get(i)).setTitle(flow.getName());
					((FielAll) this.fielAll.get(i)).setFlow(flow.getTypeName());
				}
			}
			this.totalCount = this.fielTransactService.countWorkFlowTaskFile(
					process, this.name, null, super.getStartDate(),
					super.getEndDate()).longValue();
			this.pageCount = ((this.totalCount - 1L) / 10L + 1L);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "success";
	}

	public String waitAllWorkFlowTaskFile() {
		try {
			this.flowList = this.workflowManagerService.workFlowTypeList();
			String process = this.workflowManagerService
					.getSqlAdminProcessList("task");
			this.fielAll = this.fielTransactService.getWorkFlowTaskfile(
					process, super.getCurrentPage(), this.name, null,
					super.getStartDate(), super.getEndDate());
			int i = 0;
			for (int sel = this.fielAll.size(); i < sel; i++) {
				if (isNumeric(((FielAll) this.fielAll.get(i)).getSender())) {
					String count = ((FielAll) this.fielAll.get(i)).getSender();
					((FielAll) this.fielAll.get(i)).setSender(this.taskService
							.getUserNameNew(
									Integer.parseInt(((FielAll) this.fielAll
											.get(i)).getSender())).getName());
					((FielAll) this.fielAll.get(i))
							.setUnit(this.fielTransactService
									.getUserAllOrgName(this.taskService
											.getUserNameNew(
													Integer.parseInt(count))
											.getOrgId().intValue()));
				}
				if (isNumeric(((FielAll) this.fielAll.get(i)).getFlow())) {
					OWorkflow flow = this.fielTransactService
							.getWorkFlow(Integer
									.parseInt(((FielAll) this.fielAll.get(i))
											.getFlow()));
					((FielAll) this.fielAll.get(i)).setTitle(flow.getName());
					((FielAll) this.fielAll.get(i)).setFlow(flow.getTypeName());
				}
			}
			this.totalCount = this.fielTransactService.countWorkFlowTaskFile(
					process, this.name, null, super.getStartDate(),
					super.getEndDate()).longValue();
			this.pageCount = ((this.totalCount - 1L) / 10L + 1L);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "success";
	}

	public String overWorkFlowTaskFile() {
		try {
			this.flowList = this.workflowManagerService.workFlowTypeList();
			OUser user = (OUser) getSession().get("userInfo");
			String process = this.workflowManagerService
					.getSqlDoneProcessinstanceList(user.getId().intValue());
			this.fielAll = this.fielTransactService.getWorkFlowTaskfile(
					process, super.getCurrentPage(), this.name, null,
					super.getStartDate(), super.getEndDate());
			int i = 0;
			for (int sel = this.fielAll.size(); i < sel; i++) {
				if (isNumeric(((FielAll) this.fielAll.get(i)).getSender())) {
					String count = ((FielAll) this.fielAll.get(i)).getSender();
					((FielAll) this.fielAll.get(i)).setSender(this.taskService
							.getUserNameNew(
									Integer.parseInt(((FielAll) this.fielAll
											.get(i)).getSender())).getName());
					((FielAll) this.fielAll.get(i))
							.setUnit(this.fielTransactService
									.getUserAllOrgName(this.taskService
											.getUserNameNew(
													Integer.parseInt(count))
											.getOrgId().intValue()));
				}
				if (isNumeric(((FielAll) this.fielAll.get(i)).getFlow())) {
					OWorkflow flow = this.fielTransactService
							.getWorkFlow(Integer
									.parseInt(((FielAll) this.fielAll.get(i))
											.getFlow()));
					((FielAll) this.fielAll.get(i)).setTitle(flow.getName());
					((FielAll) this.fielAll.get(i)).setFlow(flow.getTypeName());
				}
			}
			this.totalCount = this.fielTransactService.countWorkFlowTaskFile(
					process, this.name, null, super.getStartDate(),
					super.getEndDate()).longValue();
			this.pageCount = ((this.totalCount - 1L) / 10L + 1L);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "success";
	}

	public String overAllWorkFlowTaskFile() {
		try {
			this.flowList = this.workflowManagerService.workFlowTypeList();
			String process = this.workflowManagerService
					.getSqlAdminProcessList("end");
			this.fielAll = this.fielTransactService.getWorkFlowTaskfile(
					process, super.getCurrentPage(), this.name, null,
					super.getStartDate(), super.getEndDate());
			int i = 0;
			for (int sel = this.fielAll.size(); i < sel; i++) {
				if (isNumeric(((FielAll) this.fielAll.get(i)).getSender())) {
					String count = ((FielAll) this.fielAll.get(i)).getSender();
					((FielAll) this.fielAll.get(i)).setSender(this.taskService
							.getUserNameNew(
									Integer.parseInt(((FielAll) this.fielAll
											.get(i)).getSender())).getName());
					((FielAll) this.fielAll.get(i))
							.setUnit(this.fielTransactService
									.getUserAllOrgName(this.taskService
											.getUserNameNew(
													Integer.parseInt(count))
											.getOrgId().intValue()));
				}
				if (isNumeric(((FielAll) this.fielAll.get(i)).getFlow())) {
					OWorkflow flow = this.fielTransactService
							.getWorkFlow(Integer
									.parseInt(((FielAll) this.fielAll.get(i))
											.getFlow()));
					((FielAll) this.fielAll.get(i)).setTitle(flow.getName());
					((FielAll) this.fielAll.get(i)).setFlow(flow.getTypeName());
				}
			}
			this.totalCount = this.fielTransactService.countWorkFlowTaskFile(
					process, this.name, null, super.getStartDate(),
					super.getEndDate()).longValue();
			this.pageCount = ((this.totalCount - 1L) / 10L + 1L);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "success";
	}

	public String rejectWorkFlowTaskFile() {
		try {
			this.flowList = this.workflowManagerService.workFlowTypeList();
			OUser user = (OUser) getSession().get("userInfo");
			List agent = this.taskService.getAgentUser(user);
			String process = this.workflowManagerService
					.getSqlProcessinstanceList(user.getId().intValue(), agent);
			this.fielAll = this.fielTransactService.getWorkFlowTaskfile(
					process, super.getCurrentPage(), this.name, user,
					super.getStartDate(), super.getEndDate());
			int i = 0;
			for (int sel = this.fielAll.size(); i < sel; i++) {
				if (isNumeric(((FielAll) this.fielAll.get(i)).getSender())) {
					String count = ((FielAll) this.fielAll.get(i)).getSender();
					((FielAll) this.fielAll.get(i)).setSender(this.taskService
							.getUserNameNew(
									Integer.parseInt(((FielAll) this.fielAll
											.get(i)).getSender())).getName());
					((FielAll) this.fielAll.get(i))
							.setUnit(this.fielTransactService
									.getUserAllOrgName(this.taskService
											.getUserNameNew(
													Integer.parseInt(count))
											.getOrgId().intValue()));
				}
				if (isNumeric(((FielAll) this.fielAll.get(i)).getFlow())) {
					OWorkflow flow = this.fielTransactService
							.getWorkFlow(Integer
									.parseInt(((FielAll) this.fielAll.get(i))
											.getFlow()));
					((FielAll) this.fielAll.get(i)).setTitle(flow.getName());
					((FielAll) this.fielAll.get(i)).setFlow(flow.getTypeName());
				}
			}
			this.totalCount = this.fielTransactService.countWorkFlowTaskFile(
					process, this.name, user, super.getStartDate(),
					super.getEndDate()).longValue();
			this.pageCount = ((this.totalCount - 1L) / 10L + 1L);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "success";
	}

	public String hasWaitFiel() {
		try {
			OUser user = (OUser) getSession().get("userInfo");
			String process = this.workflowManagerService
					.getSqlDoneProcessinstanceList(user.getId().intValue());
			this.fielAll = this.fielTransactService.getAllfiel(process,
					super.getCurrentPage(), this.name, null,
					super.getStartDate(), super.getEndDate());
			int i = 0;
			for (int sel = this.fielAll.size(); i < sel; i++) {
				if (isNumeric(((FielAll) this.fielAll.get(i)).getSender())) {
					String count = ((FielAll) this.fielAll.get(i)).getSender();
					((FielAll) this.fielAll.get(i)).setSender(this.taskService
							.getUserNameNew(
									Integer.parseInt(((FielAll) this.fielAll
											.get(i)).getSender())).getName());
					((FielAll) this.fielAll.get(i))
							.setUnit(this.fielTransactService
									.getUserAllOrgName(this.taskService
											.getUserNameNew(
													Integer.parseInt(count))
											.getOrgId().intValue()));
				}
				if (isNumeric(((FielAll) this.fielAll.get(i)).getFlow())) {
					OWorkflow flow = this.fielTransactService
							.getWorkFlow(Integer
									.parseInt(((FielAll) this.fielAll.get(i))
											.getFlow()));
					if ((((FielAll) this.fielAll.get(i)).getTitle() == null)
							|| ("".equals(((FielAll) this.fielAll.get(i))
									.getTitle()))) {
						((FielAll) this.fielAll.get(i))
								.setTitle(flow.getName());
					}
					((FielAll) this.fielAll.get(i)).setFlow(flow.getTypeName());
				}
			}
			this.totalCount = this.fielTransactService.countAllFiel(process,
					this.name, null, super.getStartDate(), super.getEndDate())
					.longValue();
			this.pageCount = ((this.totalCount - 1L) / 10L + 1L);
			
			
			JsonConfig config = new JsonConfig();
			config.registerJsonValueProcessor(Date.class, new DateProcessor());
			config.registerJsonValueProcessor(java.util.Date.class,
					new DateProcessor());
			JSONArray array = JSONArray.fromObject(fielAll,config);
			JSONObject obj = new JSONObject();
			obj.put("totalCount", totalCount);
			obj.put("currentPage", super.getCurrentPage());
			obj.put("list", array);
			HttpServletResponse response = super.getResponse();
			response.setContentType("text/json;charset=utf-8");
			PrintWriter out = super.getResponse().getWriter();
			out.print(obj.toString());
			out.flush();
			
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public String endFiel() {
		try {
			OUser user = (OUser) getSession().get("userInfo");
			String process = this.workflowManagerService
					.getSqlDoneProcessinstanceList(user.getId().intValue());
			this.fielAll = this.fielTransactService.getAllfiel(process,
					super.getCurrentPage(), this.name, null,
					super.getStartDate(), super.getEndDate());
			int i = 0;
			for (int sel = this.fielAll.size(); i < sel; i++) {
				if (isNumeric(((FielAll) this.fielAll.get(i)).getSender())) {
					String count = ((FielAll) this.fielAll.get(i)).getSender();
					((FielAll) this.fielAll.get(i)).setSender(this.taskService
							.getUserNameNew(
									Integer.parseInt(((FielAll) this.fielAll
											.get(i)).getSender())).getName());
					((FielAll) this.fielAll.get(i))
							.setUnit(this.fielTransactService
									.getUserAllOrgName(this.taskService
											.getUserNameNew(
													Integer.parseInt(count))
											.getOrgId().intValue()));
				}
				if (isNumeric(((FielAll) this.fielAll.get(i)).getFlow())) {
					OWorkflow flow = this.fielTransactService
							.getWorkFlow(Integer
									.parseInt(((FielAll) this.fielAll.get(i))
											.getFlow()));
					if ((((FielAll) this.fielAll.get(i)).getTitle() == null)
							|| ("".equals(((FielAll) this.fielAll.get(i))
									.getTitle()))) {
						((FielAll) this.fielAll.get(i))
								.setTitle(flow.getName());
					}
					((FielAll) this.fielAll.get(i)).setFlow(flow.getTypeName());
				}
			}
			this.totalCount = this.fielTransactService.countAllFiel(process,
					this.name, null, super.getStartDate(), super.getEndDate())
					.longValue();
			this.pageCount = ((this.totalCount - 1L) / 10L + 1L);
			
			JsonConfig config = new JsonConfig();
			config.registerJsonValueProcessor(Date.class, new DateProcessor());
			config.registerJsonValueProcessor(java.util.Date.class,
					new DateProcessor());
			JSONArray array = JSONArray.fromObject(fielAll,config);
			JSONObject obj = new JSONObject();
			obj.put("totalCount", totalCount);
			obj.put("pageCount",this.pageCount);
			obj.put("currentPage", super.getCurrentPage());
			obj.put("list", array);
			HttpServletResponse response = super.getResponse();
			response.setContentType("text/json;charset=utf-8");
			PrintWriter out = super.getResponse().getWriter();
			out.print(obj.toString());
			out.flush();
			
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public String rejectFiel() {
		try {
			OUser user = (OUser) getSession().get("userInfo");
			List agent = this.taskService.getAgentUser(user);
			String process = this.workflowManagerService
					.getSqlProcessinstanceList(user.getId().intValue(), agent);
			this.fielAll = this.fielTransactService.getAllfiel(process,
					super.getCurrentPage(), this.name, user,
					super.getStartDate(), super.getEndDate());
			int i = 0;
			for (int sel = this.fielAll.size(); i < sel; i++) {
				if (isNumeric(((FielAll) this.fielAll.get(i)).getSender())) {
					String count = ((FielAll) this.fielAll.get(i)).getSender();
					((FielAll) this.fielAll.get(i)).setSender(this.taskService
							.getUserNameNew(
									Integer.parseInt(((FielAll) this.fielAll
											.get(i)).getSender())).getName());
					((FielAll) this.fielAll.get(i))
							.setUnit(this.fielTransactService
									.getUserAllOrgName(this.taskService
											.getUserNameNew(
													Integer.parseInt(count))
											.getOrgId().intValue()));
				}
				if (isNumeric(((FielAll) this.fielAll.get(i)).getFlow())) {
					OWorkflow flow = this.fielTransactService
							.getWorkFlow(Integer
									.parseInt(((FielAll) this.fielAll.get(i))
											.getFlow()));
					if ((((FielAll) this.fielAll.get(i)).getTitle() == null)
							|| ("".equals(((FielAll) this.fielAll.get(i))
									.getTitle()))) {
						((FielAll) this.fielAll.get(i))
								.setTitle(flow.getName());
					}
					((FielAll) this.fielAll.get(i)).setFlow(flow.getTypeName());
				}
			}
			this.totalCount = this.fielTransactService.countAllFiel(process,
					this.name, user, super.getStartDate(), super.getEndDate())
					.longValue();
			this.pageCount = ((this.totalCount - 1L) / 10L + 1L);
			
			
			JsonConfig config = new JsonConfig();
			config.registerJsonValueProcessor(Date.class, new DateProcessor());
			config.registerJsonValueProcessor(java.util.Date.class,
					new DateProcessor());
			JSONArray array = JSONArray.fromObject(fielAll,config);
			JSONObject obj = new JSONObject();
			obj.put("totalCount", totalCount);
			obj.put("currentPage", super.getCurrentPage());
			obj.put("pageCount", this.pageCount);
			obj.put("list", array);
			HttpServletResponse response = super.getResponse();
			response.setContentType("text/json;charset=utf-8");
			PrintWriter out = super.getResponse().getWriter();
			out.print(obj.toString());
			out.flush();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public boolean isNumeric(String str) {
		Pattern pattern = Pattern.compile("[0-9]*");
		Matcher isNum = pattern.matcher(str);

		return isNum.matches();
	}

	public FielTransactService getFielTransactService() {
		return this.fielTransactService;
	}

	public void setFielTransactService(FielTransactService fielTransactService) {
		this.fielTransactService = fielTransactService;
	}

	public WorkflowManagerService getWorkflowManagerService() {
		return this.workflowManagerService;
	}

	public void setWorkflowManagerService(
			WorkflowManagerService workflowManagerService) {
		this.workflowManagerService = workflowManagerService;
	}

	public List<FielAll> getFielAll() {
		return this.fielAll;
	}

	public void setFielAll(List<FielAll> fielAll) {
		fielAll = fielAll;
	}

	public long getTotalCount() {
		return this.totalCount;
	}

	public void setTotalCount(long totalCount) {
		this.totalCount = totalCount;
	}

	public long getPageCount() {
		return this.pageCount;
	}

	public void setPageCount(long pageCount) {
		this.pageCount = pageCount;
	}

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public TaskService getTaskService() {
		return this.taskService;
	}

	public void setTaskService(TaskService taskService) {
		this.taskService = taskService;
	}

	public List<OWorkflow> getFlowList() {
		return this.flowList;
	}

	public void setFlowList(List<OWorkflow> flowList) {
		this.flowList = flowList;
	}
}
